package com.example.themtext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Flags.Flag;
import javax.mail.search.FlagTerm;

import android.support.v7.app.ActionBarActivity;
import android.text.AndroidCharacter;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {

	private Button bt,bt1;
	private ListView lv;
	private ArrayList<String> strrArr;
	private ArrayAdapter<String> adapter;
	private EditText et;
	Folder inbox;
	private String gopthu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt =(Button)findViewById(R.id.button1);
        lv =(ListView)findViewById(R.id.listView1);
        et= (EditText)findViewById(R.id.editText1);
        bt1=(Button)findViewById(R.id.button2);
        strrArr = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,strrArr);
        lv.setAdapter(adapter);
     
        bt.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
			//	strrArr.add(et.getText().toString());
			//	adapter.notifyDataSetChanged();
				et.setText("");
				Properties props = System.getProperties();
			    props.setProperty("mail.store.protocol", "imaps");
			    try
			    {
			    	
			            Session session = Session.getDefaultInstance(props, null);
			            Store store = session.getStore("imaps");
			            store.connect("imap.gmail.com","buitrungdung5@gmail.com", "02303865786");//second is your email id and your password

			            inbox = store.getFolder("Inbox");
			            System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());

			            inbox.open(Folder.READ_ONLY);


			            Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));


			            FetchProfile fp = new FetchProfile();
			            fp.add(FetchProfile.Item.ENVELOPE);
			            fp.add(FetchProfile.Item.CONTENT_INFO);
			            inbox.fetch(messages, fp);
			        try
			            {
			                printAllMessages(messages);
			                inbox.close(true);
			                store.close();
			            }
			        catch (Exception ex)
			            {
			                System.out.println("Exception arise at the time of read mail");
			            }
			        }
			        catch (Exception e)
			            {
			                System.exit(1);
			            }
			}
			
			
			private void printAllMessages(Message[] messages) {
				for (int i = 0; i < messages.length; i++)
			    {
				//	strrArr.add("Thư :" + (i + 1) + "\n");
				//	adapter.notifyDataSetChanged();
					gopthu="";
					gopthu += "Thư :" +(i+1)+"\n";
					 printEnvelope(messages[i]);
			    }
				
			}


			private void printEnvelope(Message message) {
				Address[] a;

				try {
					if ((a = message.getFrom()) != null)
					{
					    for (Address a1 : a) {
					    //	strrArr.add("FROM: " + a1.toString()+ "\n");
					    //	adapter.notifyDataSetChanged();
					    	gopthu += "FROM: "+a1.toString()+ "\n";
					    }
					}
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				String subject = null;
				try {
					subject = message.getSubject();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Date receivedDate = null;
				try {
					receivedDate = message.getReceivedDate();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			//	strrArr.add("Subject : " + subject+ "\n");
			//	strrArr.add("Received Date : " + receivedDate.toString()+ "\n");
			//	adapter.notifyDataSetChanged();
				gopthu += "Subject : " +subject.toString()+ "\n";
				gopthu += "Received Date : " +receivedDate.toString()+ "\n";
				strrArr.add(gopthu);
				adapter.notifyDataSetChanged();
			}
		});
       
        bt1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				et.setText("Xin chao các bạn");
			}
		});
    }


	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
